﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DPS.DataModel;
using System.Configuration;
using System.Threading;
namespace DPS.Testing.DataGenerate
{
    class Program
    {
        private static string dataConnString = ConfigurationManager.ConnectionStrings["DPSConnectionString"].ConnectionString;
        private static SynchronizationContext syncConext = new SynchronizationContext();
        static void Main(string[] args)
        {
            DPSDataModelDataContext dataContext = new DPSDataModelDataContext(dataConnString);
            string batchId = "UPHBatch" + DateTime.Now.ToString("yyyyMMddhhmmss");
          dataContext.DPSBatchTs.Where(batch => batch.BatchType == "UPH").ToList().ForEach(batch => { batch.Status = "0"; });

            DPSBatchT newBatch = new DPSBatchT();
            newBatch.BatchID = batchId;
            newBatch.BatchType = "UPH";
            newBatch.CreateTime = DateTime.Now;
            newBatch.EndTime = DateTime.Now.AddDays(1);
            newBatch.Status = "1";
            newBatch.ReleasedBy = "System";
            newBatch.ReleasedDate = DateTime.Now;
            newBatch.SimulatedBy = "Systen";
            newBatch.Owner = "AHEH";
            dataContext.DPSBatchTs.InsertOnSubmit(newBatch);
            dataContext.SubmitChanges();

            dataContext.DPSProdLineByBatchTs.Where(p => p.ProdLine != null && p.ProdLine!="")
                .ToList().ForEach(line =>
                {
                     ThreadPool.QueueUserWorkItem((s) => { GenarateCapacity(DateTime.Now.AddDays(1), 60, "200.00", line.ProdLine, 0, batchId); });
                });
            System.Console.Read();
        }

        private static void GenarateCapacity(DateTime beginDate, int days, string UPH, string LineNo, int beginSortNo, string batchId)
        {


            DPSDataModelDataContext dataContext = new DPSDataModelDataContext(dataConnString);
         
            for (int day = 0; day <=days; day++)
            {
                DateTime today = beginDate.AddDays(day);
                if (today.DayOfWeek == DayOfWeek.Sunday)
                {
                    continue;
                }
                DateTime currentTime = new DateTime(today.Year, today.Month, today.Day, 0, 0, 0);
                DateTime todayEndTime = new DateTime(today.Year, today.Month, today.Day, 23, 59, 59);
                 while (currentTime <= todayEndTime.AddHours(-2))
                {
                    //DateTime beginTime = currentTime;
                    DateTime endTime = currentTime.AddHours(2);
                    DPSUPHByBatchT entity = GenarateUPDEntity(batchId, currentTime, endTime, UPH, LineNo, beginSortNo);
                    
                    dataContext.DPSUPHByBatchTs.InsertOnSubmit(entity);
                    dataContext.SubmitChanges();
                    beginSortNo++;
                    currentTime = currentTime.AddHours(2);
                    syncConext.Send((s) => { Console.WriteLine(string.Format("{0}:{1}", LineNo, beginSortNo)); },null);
                }
            }
        }

        private static DPSUPHByBatchT GenarateUPDEntity(string batchID, DateTime beginTime, DateTime endTime, string UPH, string LineNo, int SortNo)
        {
            DPSUPHByBatchT entity = new DPSUPHByBatchT();
            entity.BatchID = batchID;
            entity.BeginTime = beginTime;
            entity.EndTime = endTime;
            entity.Owner = "AHEH";
          
            entity.ProductDate = new DateTime(beginTime.Year, beginTime.Month, beginTime.Day, 0, 0, 0);
            if (beginTime.Hour <18 && beginTime.Hour >= 6)
            {
                entity.Shift = "Day";
                entity.UPH = Decimal.Parse(UPH)+(decimal)100.00;
            }
            else
            {
                entity.Shift = "Night";
                entity.UPH = Decimal.Parse(UPH);
            }
            entity.ProdLine = LineNo;
            entity.ModifiedBy = "System";
            entity.ModifiedDate = DateTime.Now;
            entity.SortNo = SortNo;
            return entity;
        }
    }
}
